Imports System.data.SqlClient

Module General
    Public string_conexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Documents\Compartido\CircuitSoft\Desarrollo\GUI\BD\BD_CircuitSoft.accdb"
    'Public string_conexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\CircuitSoft\BD\BD_CircuitSoft.accdb"
    Public conexion As New Data.OleDb.OleDbConnection
    Public cmd As New Data.OleDb.OleDbCommand
    Public tipoConsulta As Integer
    Public rutaImagenes As String = "D:\Documents\Compartido\CircuitSoft\Desarrollo\GUI\Images\"
    Public circuito As String

    Public nombreUsuario As String
    Public rol As Integer

    Public Sub main()
        Application.EnableVisualStyles() 'aca activamos estilos visuaes
        ShowSplash()
        Application.Run(New frmInicio)
    End Sub

    'VENTANA DE INICIO DE PROGRAMA
    Private Sub ShowSplash()
        Dim fSplash As New SplashScreen
        fSplash.Show()
        Application.DoEvents()
        Threading.Thread.Sleep(1000)
        fSplash.Close()
        fSplash.Dispose()
    End Sub
    'METODOS ADICIONALES 
    Public Function aleatorio(ByVal min As Integer, ByVal max As Integer) As Integer
        Return (min + Rnd() * max)
    End Function

    'PARA BASE DE DATOS'
    Public Sub insertar(ByVal insercion As String)
        conexion.ConnectionString = string_conexion
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = insercion
        cmd.ExecuteNonQuery()
        conexion.Close()
    End Sub

    Public Function ejecutarConsulta(ByVal consulta As String) As Data.DataTable
        Dim tabla As New Data.DataTable
        conexion.ConnectionString = string_conexion
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = consulta
        tabla.Load(cmd.ExecuteReader)
        conexion.Close()
        Return tabla
    End Function

    Public Sub cargarCombo(ByVal combo As ComboBox, ByVal nombreTabla As String, ByVal pk As String, ByVal desc As String)
        Dim consulta As String
        consulta = "select * from " & nombreTabla
        combo.DataSource = ejecutarConsulta(consulta)
        combo.ValueMember = pk
        combo.DisplayMember = desc
    End Sub

    Public Function obtenerTabla(ByVal nombreTabla As String) As Data.DataTable
        Dim Consulta As String
        Consulta = "select * from " & nombreTabla
        Return ejecutarConsulta(Consulta)
    End Function

    Public Function obtenerDatos(ByVal codigo As Integer, ByVal nombreTabla As String, ByVal campoId As String) As Data.DataTable
        Dim Consulta As String
        Consulta = "select * from " & nombreTabla
        Consulta &= " where " & campoId & "=" & codigo
        Consulta &= " and fechaDeBaja is null "
        Return ejecutarConsulta(Consulta)
    End Function

    'metodo para trabajar con transacciones
    Public Function ejecutarTransaccion(ByVal transaccion As String) As Boolean
        'Dim con As SqlConnection = New SqlConnection("Data Source=BELEN-PC; Initial Catalog=Ferreteria_2")
        conexion.ConnectionString = string_conexion
        conexion.Open()
        Dim miTrans As OleDb.OleDbTransaction
        'Dim comando As OleDb.OlDbCommand
        miTrans = conexion.BeginTransaction
        Try
            Dim comando As New OleDb.OleDbCommand(transaccion, conexion)
            comando.Transaction = miTrans
            comando.ExecuteNonQuery()
            miTrans.Commit()
            conexion.Close()
            Return True
        Catch ex As Exception
            miTrans.Rollback()
            conexion.Close()
            Return False
        End Try

    End Function
End Module